//
//  FindViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/27.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "FindViewController.h"
#import "UIView+SDAutoLayout.h"
#import "FindTableViewCell.h"
#import "ReplyViewController.h"

@interface FindViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (nonatomic , strong)UISegmentedControl *messageControl;
//@property (nonatomic , strong)NSArray *messageArray;

@property (weak, nonatomic) IBOutlet UITableView *hotTV;
@property (weak, nonatomic) IBOutlet UITableView *recentlyTV;
@property (weak, nonatomic) IBOutlet UITableView *attentionTV;

@property (nonatomic,strong)NSMutableArray *hotArray;
@property (nonatomic,strong)NSMutableArray *recentlyArray;
@property (nonatomic,strong)NSMutableArray *attentionArray;

@property (weak, nonatomic) IBOutlet UIScrollView *scroV;

@property (nonatomic,strong)NSMutableArray *nameArray1;
@property (nonatomic,strong)NSMutableArray *nameArray2;
@property (nonatomic,strong)NSMutableArray *nameArray3;

@property (nonatomic,strong)NSMutableArray *messageArray1;
@property (nonatomic,strong)NSMutableArray *messageArray2;
@property (nonatomic,strong)NSMutableArray *messageArray3;
@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    self.title = @"发现";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.messageControl = [[UISegmentedControl alloc]initWithItems:@[@"最热",@"最新",@"关注"]];
    self.navigationItem.titleView = self.messageControl;
    self.messageControl.selectedSegmentIndex = 0;
    
    [self.messageControl addTarget:self action:@selector(changeAction:) forControlEvents:(UIControlEventValueChanged)];
    //设置格式
    self.messageControl.sd_layout
    .heightIs(30)
    .widthIs([UIScreen mainScreen].bounds.size.width*0.5);
    
    UINib *nib = [UINib nibWithNibName:@"FindTableViewCell" bundle:nil];
    
    [self.hotTV registerNib:nib forCellReuseIdentifier:@"FindListCell"];
    [self.recentlyTV registerNib:nib forCellReuseIdentifier:@"FindListCell"];
    [self.attentionTV registerNib:nib forCellReuseIdentifier:@"FindListCell"];
    
    self.hotTV.delegate = self;
    self.hotTV.dataSource = self;
    self.recentlyTV.delegate = self;
    self.recentlyTV.dataSource = self;
    self.attentionTV.delegate = self;
    self.attentionTV.dataSource = self;
    self.scroV.delegate = self;
    
    self.scroV.pagingEnabled = YES;
    
    for (int i = 1; i <11; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d.jpg",i];
        [self.hotArray addObject:imageName];
    }
    for (int i = 11; i <21; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d.jpg",i];
        [self.recentlyArray addObject:imageName];
    }
    for (int i = 21; i <31; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d.jpg",i];
        [self.attentionArray addObject:imageName];
    }
    
    self.nameArray1 = [NSMutableArray arrayWithObjects:@"大哥",@"第二梦",@"晴晴",@"大宝",@"菇凉",@"海子",@"温暖",@"白雪",@"热血",@"大大", nil];
    self.nameArray2 = [NSMutableArray arrayWithObjects:@"小二",@"疯狗",@"晴晴",@"大宝",@"菇凉",@"海子",@"温暖",@"白雪",@"热血",@"第二梦", nil];
    self.nameArray3 = [NSMutableArray arrayWithObjects:@"小小",@"第二梦",@"晴晴",@"大宝",@"菇凉",@"海子",@"温暖",@"白雪",@"热血",@"小三", nil];
    
    self.messageArray1 = [NSMutableArray arrayWithObjects:@"谁能听懂我的方言",@"我终于睡醒了",@"来聊聊吧",@"冒个泡，咕噜咕噜",@"谁来为我唱歌",@"不知道说什么",@"你的月亮我的心",@"我适合唱谁的歌，来说说",@"我喜欢暖暖的声音，有木有",@"我喜欢好听的声音，求勾搭", nil];
    self.messageArray2 = [NSMutableArray arrayWithObjects:@"谁能听懂我的方言",@"我终于睡醒了",@"来聊聊吧",@"冒个泡，咕噜咕噜",@"谁来为我唱歌",@"不知道说什么",@"好无聊，谁来陪我聊天",@"为什么总是闪退",@"冒个泡，咕噜咕噜",@"谁能听懂我的方言", nil];
    self.messageArray3 = [NSMutableArray arrayWithObjects:@"谁能听懂我的方言",@"我终于睡醒了",@"来聊聊吧",@"冒个泡，咕噜咕噜",@"谁来为我唱歌",@"不知道说什么",@"我愿意倾听你们的烦恼",@"哎呀，好听",@"我好困，睡了晚安",@"来聊聊吧", nil];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.messageControl.selectedSegmentIndex = self.scroV.contentOffset.x/[UIScreen mainScreen].bounds.size.width;
}

-(void)changeAction:(UISegmentedControl *)sender{
    [self.scroV setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width*sender.selectedSegmentIndex, 0) animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.hotTV]) {
        return self.hotArray.count;
    }else if ([tableView isEqual:self.recentlyTV]){
        return self.recentlyArray.count;
    }else
        return self.attentionArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FindListCell"];
    
    if ([tableView isEqual:self.hotTV]) {
        cell.timeLabel.text = [NSString stringWithFormat:@"%ld回复",199-indexPath.row];
        cell.messageLabel.text = self.messageArray1[self.messageArray1.count-1-indexPath.row];
        cell.nameLabel.text = self.nameArray1[self.nameArray1.count-1-indexPath.row];
        [cell.imageBtn setBackgroundImage:[UIImage imageNamed:self.hotArray[self.hotArray.count-1-indexPath.row]] forState:(UIControlStateNormal)];
    }else if ([tableView isEqual:self.recentlyTV]){
        cell.timeLabel.text = [NSString stringWithFormat:@"%ld分钟前",indexPath.row+1];
        cell.messageLabel.text = self.messageArray2[self.messageArray2.count-1-indexPath.row];
        cell.nameLabel.text = self.nameArray2[self.nameArray2.count-1-indexPath.row];
        [cell.imageBtn setBackgroundImage:[UIImage imageNamed:self.recentlyArray[self.recentlyArray.count-1-indexPath.row]] forState:(UIControlStateNormal)];
    }else{
        
        cell.timeLabel.text = [NSString stringWithFormat:@"昨天%02d:%02d",arc4random()%24,arc4random()%60];
        cell.nameLabel.text = self.nameArray3[self.nameArray3.count-1-indexPath.row];
        cell.messageLabel.text = self.messageArray3[self.messageArray3.count-1-indexPath.row];
        [cell.imageBtn setBackgroundImage:[UIImage imageNamed:self.attentionArray[self.attentionArray.count-1-indexPath.row]] forState:(UIControlStateNormal)];
    }
    cell.timeLabel.textAlignment = NSTextAlignmentCenter;
    cell.imageBtn.layer.masksToBounds = YES;
    cell.imageBtn.layer.cornerRadius =  cell.imageBtn.frame.size.width/2.0;
    cell.imageBtn.tag = indexPath.row +101;
    [cell.imageBtn addTarget:self action:@selector(listenVoice) forControlEvents:(UIControlEventTouchUpInside)];
    
    cell.messageLabel.numberOfLines = 0;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReplyViewController *replyVC = [ReplyViewController new];
    if (self.messageControl.selectedSegmentIndex == 0) {
        replyVC.imageName = self.hotArray[self.hotArray.count-1-indexPath.row];
        replyVC.nick = self.nameArray1[self.nameArray1.count-1-indexPath.row];
        replyVC.message = self.messageArray1[self.messageArray1.count-1-indexPath.row];
    }else if (self.messageControl.selectedSegmentIndex == 1){
        replyVC.imageName = self.recentlyArray[self.recentlyArray.count-1-indexPath.row];
        replyVC.nick = self.nameArray2[self.nameArray2.count-1-indexPath.row];
        replyVC.message = self.messageArray2[self.messageArray2.count-1-indexPath.row];
    }else{
        replyVC.imageName = self.attentionArray[self.attentionArray.count-1-indexPath.row];
        replyVC.nick = self.nameArray3[self.nameArray3.count-1-indexPath.row];
        replyVC.message = self.messageArray3[self.messageArray3.count-1-indexPath.row];
    }
    [self.navigationController pushViewController:replyVC animated:YES];
}

-(void)listenVoice{
    NSLog(@"播放语音，待添加");
}

-(NSMutableArray *)hotArray{
    if (!_hotArray) {
        _hotArray = [NSMutableArray array];
    }
    return _hotArray;
}
-(NSMutableArray *)recentlyArray{
    if (!_recentlyArray) {
        _recentlyArray = [NSMutableArray array];
    }
    return _recentlyArray;
}
-(NSMutableArray *)attentionArray{
    if (!_attentionArray) {
        _attentionArray = [NSMutableArray array];
    }
    return _attentionArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [self.tabBarController.tabBar setHidden:NO];
}
//-(void)viewWillDisappear:(BOOL)animated{
//    [self.tabBarController.tabBar setHidden:YES];
//}
@end
